package com.hundsun.yr.universal.library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cfca.mobile.sipedit.SipEditText;
import com.hundsun.yr.universal.library.R;
import com.hundsun.yr.universal.library.common.CPResourceUtil;

/**
 * @Title: HSSipEditText.java
 * @Description: 密码输入框
 * @date 2015-12-1 上午10:18:50
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 * @version V1.0
 */
public class HSSipEditText extends LinearLayout implements OnClickListener ,TextWatcher ,OnFocusChangeListener{

	
	/**眼睛按钮是否选中**/
	private boolean openEyes = false;
	/**眼睛图片**/
	private ImageView iv_eye = null;
	/**删除图片**/
	private ImageView iv_close = null;
	/**密码输入框**/
	private SipEditText mSipEditText;
	/**mSipEditText hint文字颜色**/
	private int hintTextColor = -1;
	/**hint文字**/
	private CharSequence mQueryHint;
	/**hint文字字体大小**/
    private int hintTextSize=0;
    /** mSipEditText 文字颜色**/
	private int textColor = -1;
	/** mSipEditText 文字大小*/
	private int textSize = 0;
	/**背景色**/
	private int bgColor=-1;
	/**眼睛按下图片**/
	private int eye_p=0;
	/**眼睛图片 **/
	private int eye_n=0;

	public HSSipEditText(Context context) {
		this(context, null);
	}

	public HSSipEditText(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public HSSipEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs);
	}

	private void init(Context context, AttributeSet attrs) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_hssipedittext, this, true);

		iv_eye=(ImageView)findViewById(R.id.iv_eye);
		iv_close=(ImageView)findViewById(R.id.iv_close);
		mSipEditText=(SipEditText)findViewById(R.id.et_password);
		mSipEditText.addTextChangedListener(this);
//		mSipEditText.setEncryptState(false);
		mSipEditText.setKeyAnimation(true);
		mSipEditText.setOnFocusChangeListener(this);
		iv_eye.setOnClickListener(this);
		iv_close.setOnClickListener(this);
		setOutSideDisappear(true);
		eye_p=CPResourceUtil.getDrawableId(context, "icon_eye_p");
		eye_n=CPResourceUtil.getDrawableId(context, "icon_eye_n");
		
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HSSipEditText);
		if (a.hasValue(R.styleable.HSSipEditText_sip_hintTextColor)) {
			hintTextColor = a.getColor(R.styleable.HSSipEditText_sip_hintTextColor, -1);
			setQueryHintColor(hintTextColor);
		}

		if (a.hasValue(R.styleable.HSSipEditText_sip_hint)) {
			mQueryHint = a.getString(R.styleable.HSSipEditText_sip_hint);
			setQueryHint(mQueryHint);
		}
		
		if (a.hasValue(R.styleable.HSSipEditText_sip_textColor)) {
			textColor = a.getColor(R.styleable.HSSipEditText_sip_textColor, -1);
			if (textColor != -1) {
				mSipEditText.setTextColor(textColor);
			}
		}
		
		if (a.hasValue(R.styleable.HSSipEditText_sip_textSize)) {
			textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, a.getDimension(R.styleable.HSSipEditText_sip_textSize, 0), getResources().getDisplayMetrics());
			if (textSize != 0) {
				mSipEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
			}
		}
		
		if (a.hasValue(R.styleable.HSSipEditText_sip_openEyes)) {
			openEyes = a.getBoolean(R.styleable.HSSipEditText_sip_openEyes, false);
			iv_eye.setImageResource(openEyes?eye_p:eye_n);
			openEyes(openEyes);
		}
		
		if (a.hasValue(R.styleable.HSSipEditText_sip_maxLength)) {
			int maxLength = a.getInt(R.styleable.HSSipEditText_sip_maxLength, 100);
			setPasswordMaxLength(maxLength);
		}
		
		if (a.hasValue(R.styleable.HSSipEditText_sip_keyBoardType)) {
			int keyBoardType = a.getInt(R.styleable.HSSipEditText_sip_keyBoardType, 0);
			setSipKeyBoardType(keyBoardType);
		}
		
		
		if (a.hasValue(R.styleable.HSSipEditText_sip_isNumDisorder)) {
			boolean isNumDisorder = a.getBoolean(R.styleable.HSSipEditText_sip_isNumDisorder, false);
			setNumDisorder(isNumDisorder);
		}
		
		if (a.hasValue(R.styleable.HSSipEditText_sip_isOutSideDisappear)) {
			boolean sip_isOutSideDisappear = a.getBoolean(R.styleable.HSSipEditText_sip_isOutSideDisappear, false);
			setOutSideDisappear(sip_isOutSideDisappear);
		}
		
		if (a.hasValue(R.styleable.HSSipEditText_sip_bgColor)) {
			bgColor = a.getColor(R.styleable.HSSipEditText_sip_bgColor, -1);
			mSipEditText.setBackgroundColor(bgColor);
		}

		a.recycle();
	}
	
	

	/**
     * 设置hint文字颜色
     */
    private CharSequence getDecoratedHint(CharSequence hintText) {
        Spannable ssb = new SpannableString(hintText);
        if(hintTextColor==-1){
        	return ssb;
        }
        ssb.setSpan(new ForegroundColorSpan(hintTextColor), 0, hintText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        if(hintTextSize>0){
        	ssb.setSpan(new AbsoluteSizeSpan(hintTextSize, true), 0, hintText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ssb;
    }
    
    
    /**
     * 设置hint文字
     * @param hint
     */
    public void setQueryHint(CharSequence hint) {
        updateQueryHint(hint);
    }
    
    
    /**
     * 设置hint文字颜色
     */
    public void setQueryHintColor(int color) {
    	hintTextColor = color;
        updateQueryHint(mSipEditText.getHint());
    }
    
    private void updateQueryHint(CharSequence hint) {
        if (hint != null) {
        	mSipEditText.setHint(getDecoratedHint(hint));
        } else {
        	mSipEditText.setHint(getDecoratedHint(""));
        }
    }
	
	/**
	 * 设置最大字符数
	 * 
	 * @param maxLength
	 */
	public void setPasswordMaxLength(int maxLength) {
		mSipEditText.setPasswordMaxLength(maxLength);
	}


	/**
	 * 获取SipEditText对象
	 * 
	 * @return
	 */
	public SipEditText getSipEditText() {
		if (mSipEditText == null) {
			throw new IllegalArgumentException("You must create a new HSEditText");
		}

		return mSipEditText;
	}



	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		if(v==iv_eye){
			if(openEyes){
				openEyes=false;
			}else{
				openEyes=true;
				
			}
			openEyes(openEyes);
			iv_eye.setImageResource(openEyes?eye_p:eye_n);
			
		}else if(v==iv_close){
			mSipEditText.setText("");
			
		}


	}
	
	
	/**
	 * 设置键盘类型
	 * @param sipKeyBoardType {@link SipEditText}}
	 */
	public void setSipKeyBoardType(int sipKeyBoardType){
		mSipEditText.setSipKeyBoardType(sipKeyBoardType);
	}
	/**
	 * 设置键盘外部点击是否消失
	 * @param isOutSideDisappear
	 */
	public void setOutSideDisappear(boolean isOutSideDisappear){
		mSipEditText.setOutSideDisappear(isOutSideDisappear);
	}
	
	/**
	 * 获取键盘外部点击是否消失
	 * @return
	 */
	public boolean isOutSideDisappear(){
		return mSipEditText.isOutSideDisappear();
	}
	
	/**
	 * 获取数字键盘是否乱序
	 * @return
	 */
	public boolean isNumDisorder(){
		return mSipEditText.isNumDisorder();
	}
	
	/**
	 * 设置数字键盘是否乱序
	 * @param isNumDisorder
	 */
	public void setNumDisorder(boolean isNumDisorder){
		mSipEditText.setNumDisorder(isNumDisorder);
	}
	
	/**
	 * 获取输入内容长度
	 */
	public int getInputLength(){
		
		return mSipEditText.getInputLength();
	}
	/**
	 * 清空所有输入数据
	 */
	public void clear(){
		mSipEditText.clear();
	}
	
	/**
	 * 键盘是否显示
	 */
	public boolean isKeyBoardShowing(){
		return mSipEditText.isKeyBoardShowing();
	}
	
	/**
	 * 获取键盘高度
	 */
	public int getKeyBoardHeight(){
		return mSipEditText.getKeyBoardHeight();
	}
	/**
	 * 明/密文转换
	 * @param open
	 */
	public void openEyes(boolean open){
		try {
			mSipEditText.openEyes(open);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 获取EditText的值
	 * 
	 * @return
	 */
	public String getText() {

		openEyes(true);
		String result= mSipEditText.getText().toString();
		openEyes(openEyes);
		return result;
		
	}

	/**
	 * 设置文本
	 * 
	 * @param res
	 */
	public void setText(int res) {
		mSipEditText.setText(res);
	}

	/**
	 * 设置文本
	 * 
	 * @param text
	 */
	public void setText(String text) {
		mSipEditText.setText(text);
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		iv_close.setVisibility(s.length() > 0?View.VISIBLE:View.GONE);
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		
	}
	
	
	/**
	 * 当ClearEditText焦点发生变化的时候，判断里面字符串长度设置清除图标的显示与隐藏
	 */
	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (hasFocus) {
			// 设置焦点
			iv_close.setVisibility(mSipEditText.getText().length() > 0?View.VISIBLE:View.GONE);
		} else {
			iv_close.setVisibility(View.GONE);
		}
	}

}
